package com.ycg.biquge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycg.biquge.common.Result;
import com.ycg.biquge.common.UserContext;
import com.ycg.biquge.pojo.BookCase;
import com.ycg.biquge.pojo.Novel;
import com.ycg.biquge.service.BookCaseService;
import com.ycg.biquge.mapper.BookCaseMapper;
import com.ycg.biquge.service.NovelService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
* @author lenovo
* @description 针对表【book_case(书架表)】的数据库操作Service实现
* @createDate 2024-04-30 20:20:39
*/
@Service
public class BookCaseServiceImpl extends ServiceImpl<BookCaseMapper, BookCase>
    implements BookCaseService{

    @Resource
    private NovelService novelService;

    /**
     * 添加书架记录
     *
     * @param bookCase 小说id + 作者 + 小说名
     * @return 成功
     */
    @Override
    public Result<String> addBookcase(BookCase bookCase) {
        Integer userId = UserContext.getUser().getId();
        bookCase.setUserId(userId);
        // 获取最新章节信息
        LambdaQueryWrapper<Novel> novelWrapper = new LambdaQueryWrapper<>();
        novelWrapper.eq(Novel::getId,bookCase.getNovelId())
                .select(Novel::getNewId,Novel::getNewChapter);
        Novel novel = novelService.getOne(novelWrapper);

        bookCase.setNewId(novel.getNewId());
        bookCase.setNewChapter(novel.getNewChapter());
        LambdaUpdateWrapper<BookCase> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(BookCase::getUserId,userId).eq(BookCase::getNovelId,bookCase.getNovelId());
        saveOrUpdate(bookCase,wrapper);
        return Result.ok("保存成功");

    }
}




